package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper,WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    public boolean deleteByid(Integer id) {
        int i = wmSensitiveMapper.deleteById(id);
        if (i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public ResponseResult Save(WmSensitive wmSensitive) {

        wmSensitive.setCreatedTime(new Date());

        int insert = wmSensitiveMapper.insert(wmSensitive);
        if (insert != 0){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);


    }

    @Override
    public ResponseResult Update(WmSensitive wmSensitive) {
        //        传过来的数据进行判断是否为空
        if (wmSensitive != null){

            wmSensitiveMapper.updateById(wmSensitive);

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }

    @Override
    public ResponseResult SelectLike(Map map) {
//            map获取到的数据是name，page，size
            String name = (String) map.get("name");
            Integer pagedto = (Integer) map.get("page");
            if (pagedto==null){
                pagedto=1;
            }
            Integer sizedto = (Integer) map.get("size");
        if (sizedto==null){
            sizedto=10;
        }
//            Ipage是Page的父类 一样用来装page参数
            IPage iPage =new Page(pagedto,sizedto);
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
//            添加queryWrapper限制条件，模糊查询 名字不为空则赋值
            queryWrapper.like(name != null,
                    WmSensitive::getSensitives,name);
//              以创建时间为升序的方式
            queryWrapper.orderByAsc(WmSensitive::getCreatedTime);
//                selectPage是分页查询，把page参数和限制条件传进去
            iPage = wmSensitiveMapper.selectPage(iPage, queryWrapper);
//            返回结果
            ResponseResult responseResult = new PageResponseResult(
                    pagedto,sizedto, (int) iPage.getTotal());
//                前端不能直接获取ipage的数据，必须getRecords获取list数据返回
            responseResult.setData(iPage.getRecords());


            return responseResult;
    }

    /**
     * 查询所有敏感词
     */
    @Override
    public ResponseResult querySensitiveAll() {

        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery());
        List<String> sensitiveStrList =wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());

        return ResponseResult.okResult(sensitiveStrList);
    }
}


